package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.UpdateDeptBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.DeptDao;
import com.zhuss.core.db.entity.Dept;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建部门 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("deptUpdate")
public class DeptUpdate extends AbstractProcessor<Object, UpdateDeptBo> implements Processor<Object, UpdateDeptBo> {

    @Autowired
    private DeptDao deptDao;
    
    @Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateDeptBo bo) throws AabException {
    	Long deptId = bo.getDeptId();
    	String name = bo.getName();
    	Long companyId = bo.getCompanyId();
    	if (deptId == null || deptId <= 0l) {
    		throw new AabException(ExceptionCode.部门ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		throw new AabException(ExceptionCode.部门name为空);
    	}
    	
    	List<Dept> list = deptRead.listNotId(companyId, name, deptId);
    	if (!list.isEmpty()) {
    		throw new AabException(ExceptionCode.部门name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateDeptBo param) {
    	Dept record = new Dept();
    	record.setDeptId(param.getDeptId());
    	record.setName(param.getName());
    	record.setSort(param.getSort());
    	    	
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	deptDao.updateByPrimaryKeySelective(record);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateDeptBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.部门修改.key());
    	record.setTargetId(param.getDeptId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
